package com.fr.report.io;

import com.fr.base.core.GraphHelper;
import com.fr.report.PageSet;
import com.fr.report.PageSetChain;
import com.fr.report.ReportPage;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.ReportUtils;
import com.fr.report.painter.PagePainter;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class ImageExporter extends AbstractAppExporter
{
  private String suffix;

  public ImageExporter()
  {
    this.suffix = "jpg";
  }

  public ImageExporter(String paramString)
  {
    setSuffix(paramString);
  }

  public void setSuffix(String paramString)
  {
    this.suffix = paramString;
  }

  public String getSuffix()
  {
    return this.suffix;
  }

  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    PageSet localPageSet = paramResultWorkBook.generateReportPageSet(ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook)).getTraversingCase();
    export(paramOutputStream, localPageSet);
    localPageSet.release();
  }

  public void export(OutputStream paramOutputStream, PageSet paramPageSet)
    throws Exception
  {
    int i = 0;
    int j = 0;
    ArrayList localArrayList = new ArrayList();
    for (int k = 0; k < paramPageSet.size(); ++k)
    {
      localObject = paramPageSet.getPage(k);
      if (localObject == null)
        break;
      i = Math.max(i, ((ReportPage)localObject).getPageWidth());
      j += ((ReportPage)localObject).getPageHeight();
      localArrayList.add(localObject);
    }
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage(i, j);
    Object localObject = localBufferedImage.createGraphics();
    int l = 0;
    int i1 = 0;
    int i2 = localArrayList.size();
    while (i1 < i2)
    {
      ReportPage localReportPage = (ReportPage)localArrayList.get(i1);
      ((Graphics2D)localObject).translate(0, l);
      new PagePainter(localReportPage, (Graphics2D)localObject, false).convert();
      ((Graphics2D)localObject).translate(0, -l);
      l += localReportPage.getPageHeight();
      ++i1;
    }
    ImageIO.write(localBufferedImage, (this.suffix == null) ? "jpg" : this.suffix, paramOutputStream);
  }
}